home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / SocketReader.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  1.7 KB  |  72 lines

  1. package symantec.itools.db.net;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6.  
  7. class SocketReader extends Thread {
  8.    MPlex _mplex;
  9.    InputStream _istrm;
  10.    String _problem;
  11.    byte[] _header;
  12.    int _msgnum;
  13.  
  14.    SocketReader(MPlex mplex, InputStream stream) {
  15.       this._mplex = mplex;
  16.       this._istrm = stream;
  17.       this._header = new byte[5];
  18.    }
  19.  
  20.    public void run() {
  21.       while(true) {
  22.          String s = null;
  23.          short streamID = (short)32767;
  24.  
  25.          try {
  26.             int lenRecv = 0;
  27.  
  28.             for(int npass = 0; lenRecv < 5 && npass < 4; ++npass) {
  29.                lenRecv += this._istrm.read(this._header, lenRecv, 5 - lenRecv);
  30.             }
  31.  
  32.             if (this._header[0] == -69) {
  33.                int msglen = this._header[2];
  34.                if (msglen < 0) {
  35.                   msglen += 256;
  36.                }
  37.  
  38.                msglen += (this._header[1] << 8) - 2;
  39.                streamID = (short)this._header[4];
  40.                if (streamID < 0) {
  41.                   streamID += 256;
  42.                }
  43.  
  44.                streamID = (short)(streamID + (this._header[3] << 8));
  45.                int ipos = 0;
  46.                byte[] msgbufRecv = new byte[msglen];
  47.  
  48.                int savedMsglen;
  49.                for(savedMsglen = msglen; msglen > 0; ipos += lenRecv) {
  50.                   lenRecv = this._istrm.read(msgbufRecv, ipos, msglen);
  51.                   msglen -= lenRecv;
  52.                }
  53.  
  54.                if (streamID == 32766) {
  55.                   throw new IOException("IDLE TIMEOUT");
  56.                }
  57.  
  58.                OutputStream ostr = this._mplex.getStream(streamID);
  59.                if (ostr != null) {
  60.                   ostr.write(msgbufRecv, 0, savedMsglen);
  61.                }
  62.             }
  63.          } catch (IOException var10) {
  64.             streamID = (short)32767;
  65.             String var11 = null;
  66.             this._mplex.connectionLost(this);
  67.             return;
  68.          }
  69.       }
  70.    }
  71. }
  72.